package cn.wolfcode.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.wolfcode.business.domain.Customer;
import cn.wolfcode.business.mapper.CustomerMapper;
import cn.wolfcode.business.vo.CustomerVO;
import cn.wolfcode.common.core.domain.entity.SysUser;
import cn.wolfcode.common.utils.SecurityUtils;
import cn.wolfcode.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.RecordMapper;
import cn.wolfcode.business.domain.Record;
import cn.wolfcode.business.service.IRecordService;

/**
 * 拜访客户Service业务层处理
 * 
 * @author 2
 * @date 2023-11-05
 */
@Service
public class RecordServiceImpl implements IRecordService 
{
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 查询拜访客户
     * 
     * @param id 拜访客户主键
     * @return 拜访客户
     */
    @Override
    public Record selectRecordById(Long id)
    {
        return recordMapper.selectRecordById(id);
    }

    /**
     * 查询拜访客户列表
     * 
     * @param record 拜访客户
     * @return 拜访客户
     */
    @Override
    public CustomerVO selectRecordList(Record record)
    {

        CustomerVO vo = new CustomerVO();
        List<SysUser> sysUserList = userMapper.selectUserNameList();
        List<String> usernames = new ArrayList<>();
        for (SysUser sysUser : sysUserList) {
            String userName = sysUser.getUserName();
            usernames.add(userName);
        }
        vo.setInputUserNames(usernames);
        List<Customer> Customers = customerMapper.selectCustomer();
        List<String> customer1 = new ArrayList<>();
        for (Customer customer : Customers) {
            String customerName = customer.getCustomerName();
            customer1.add(customerName);
        }
        vo.setCustomerNames(customer1);
        List<Record> records = recordMapper.selectRecordList(record);
        vo.setRecords(records);
        return vo;
    }

    /**
     * 新增拜访客户
     * 
     * @param record 拜访客户
     * @return 结果
     */
    @Override
    public int insertRecord(Record record)
    {
        //查询录入人
        record.setInputUserId(SecurityUtils.getUsername());
        //录入时间
        record.setEnterTime(new Date());
        return recordMapper.insertRecord(record);
    }

    /**
     * 修改拜访客户
     * 
     * @param record 拜访客户
     * @return 结果
     */
    @Override
    public int updateRecord(Record record)
    {
        return recordMapper.updateRecord(record);
    }

    /**
     * 批量删除拜访客户
     * 
     * @param ids 需要删除的拜访客户主键
     * @return 结果
     */
    @Override
    public int deleteRecordByIds(Long[] ids)
    {
        return recordMapper.deleteRecordByIds(ids);
    }

    /**
     * 删除拜访客户信息
     * 
     * @param id 拜访客户主键
     * @return 结果
     */
    @Override
    public int deleteRecordById(Long id)
    {
        return recordMapper.deleteRecordById(id);
    }
}
